package com.fis.vaadin.util;

import java.util.HashMap;
import java.util.Vector;

import com.fis.mca.admin.AppClient;
import com.fis.vaadin.component.FMenuItemCommand;
import com.fis.vaadin.component.TreeItemId;
import com.fss.util.StringUtil;
import com.vaadin.data.Item;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.Tree;
import com.vaadin.ui.MenuBar.MenuItem;

import eu.livotov.tpt.i18n.TM;

public class ModuleManager
{
	private static HashMap<String, MenuBar.MenuItem> mapMenu = new HashMap<String, MenuBar.MenuItem>();
	private static HashMap<String, TreeItemId> mapTreeItem = new HashMap<String, TreeItemId>();
	
	public static void clearMenu()
	{
		mapMenu.clear();
	}
	
	public static MenuBar.MenuItem getMenuItem(String menuName)
	{
		return mapMenu.get(menuName);
	}
	
	public static MenuBar buildMenuBar(MenuBar menubar, Vector vt)
	{
		clearMenu();
		
		loadMenu(menubar, menubar, vt);
		
		return menubar;
	}
	
	public static Tree buildTree(Tree tree, Object parent, Vector vtMenu)
	{
		for(int iIndex = 0;iIndex < vtMenu.size();iIndex++)
		{
			Vector vtInfo = (Vector)vtMenu.elementAt(iIndex);
			String strMenuName = StringUtil.nvl(vtInfo.elementAt(1),"");
			String strMenuType = StringUtil.nvl(vtInfo.elementAt(2),"");
			
			TreeItemId itemId = new TreeItemId(strMenuName, tree);
			itemId.setCaption(TM.get(strMenuName));
			itemId.setModuleType(strMenuType);
//			tree.get
			if(strMenuType.equals("G"))
			{
				itemId.setIcon(FisDefaultTheme.ICON_PACKAGE);
				Item item = tree.addItem(itemId);
		
				tree.setParent(itemId, parent);
				tree.setChildrenAllowed(itemId, true);
				buildTree(tree, itemId, (Vector)vtInfo.elementAt(3));
				tree.expandItemsRecursively(itemId);
			}
			else if(strMenuType.equals("M"))
			{
				itemId.setIcon(FisDefaultTheme.ICON_ACTION);
				
				Item item = tree.addItem(itemId);
				tree.setParent(itemId, parent);
				tree.setChildrenAllowed(itemId, false);
				mapTreeItem.put(strMenuName, itemId);
			}
		}
		
		return tree;
	}
	
	private static void loadMenu(Object topItem, MenuBar menubar, Vector vtMenu)
	{
		for(int iIndex = 0;iIndex < vtMenu.size();iIndex++)
		{
			Vector vtInfo = (Vector)vtMenu.elementAt(iIndex);
			String strMenuName = StringUtil.nvl(vtInfo.elementAt(1),"");
			String strMenuType = StringUtil.nvl(vtInfo.elementAt(2),"");

			if(strMenuType.equals("G"))
			{
				MenuBar.MenuItem item = null;
				if (topItem.equals(menubar))
				{
					item = menubar.addItem(strMenuName, null);
				}
				else
				{
					item = ((MenuBar.MenuItem)topItem).addItem(strMenuName, null);
				}
				
				loadMenu(item, menubar, (Vector)vtInfo.elementAt(3));
				mapMenu.put(strMenuName, item);
			}
			else if(strMenuType.equals("M"))
			{
				MenuBar.MenuItem item = null;
				if (topItem.equals(menubar))
				{
					item = menubar.addItem(TM.get(strMenuName),null);
				}
				else
				{
					item = ((MenuBar.MenuItem)topItem).addItem(TM.get(strMenuName),null);
				}
				
				item.setCommand(mapTreeItem.get(strMenuName));
				
				item.setEnabled(AppClient.getPermission(strMenuName).contains("S"));
				item.setDescription(TM.get(strMenuName));
				mapMenu.put(strMenuName, item);
			}
			else if(strMenuType.equals("S"))
			{
				((MenuBar.MenuItem)topItem).addSeparator();
			}
		}
	}
	
	public static void showSize()
	{
		System.out.println("map menu: " + mapMenu.size());
		System.out.println("map tree item: " + mapTreeItem.size());
	}
}
